﻿using ActivationJetbrains.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ActivationJetbrains
{
    public partial class ActiveJetBrains : Form
    {
        public ActiveJetBrains()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 选择目录按钮被单击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSelectDic_Click(object sender, EventArgs e)
        {
            // 弹出文件夹浏览对话框
            FolderBrowserDialog fbDialog = new FolderBrowserDialog();
            fbDialog.Description = "请选择Jetbrains安装路径：";
            if (fbDialog.ShowDialog() == DialogResult.OK)
            {
                txtPath.Text = fbDialog.SelectedPath;
            }
        }

        /// <summary>
        /// 寻找注册文件
        /// </summary>
        /// <returns></returns>
        public List<FileInfo> FindRegisterFile()
        {
            // 软件目录
            DirectoryInfo theDir = new DirectoryInfo(txtPath.Text);
            FileInfo[] files = theDir.GetFiles();

            List<FileInfo> fileList = new List<FileInfo>();
            // 找到注册文件
            foreach (var item in files)
            {
                if (item.Name.Contains("vmoptions"))
                {
                    fileList.Add(item);
                }
            }

            return fileList;
        }

        /// <summary>
        /// 注册补丁
        /// </summary>
        /// <returns></returns>
        public bool RegisterCrack()
        {
            try
            {
                var hackerJar = Properties.Resources.JetbrainsCrack_2_7;
                File.WriteAllBytes(Path.Combine(txtPath.Text, "JetbrainsCrack_2_7.jar"), hackerJar);
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 注册文件
        /// </summary>
        /// <param name="fileList">注册文件列表</param>
        /// <returns></returns>
        public bool WriterCrack(List<FileInfo> fileList)
        {
            // 给每个注册文件加入注册代码
            foreach (var item in fileList)
            {
                try
                {
                    var sw = item.AppendText();
                    sw.WriteLine($"-javaagent:{Path.Combine(txtPath.Text, "JetbrainsCrack_2_7.jar")}");
                    sw.Flush();
                    sw.Close();
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 注册按钮被单击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRegisterHosts_Click(object sender, EventArgs e)
        {
            List<FileInfo> fileList = FindRegisterFile();
            if (fileList.Count == 0)
            {
                MessageBox.Show("安装目录错误！没有找到*.vmoptions文件!");
            }
            if (!RegisterCrack())
            {
                MessageBox.Show("没有目录访问权限！请使用管理员身份打开软件！");
            }
            if (!WriterCrack(fileList))
            {
                MessageBox.Show("注册破解信息失败！请检查目录可用性！");
            }
            else
            {
                MessageBox.Show("注册破解信息成功！请复制注册码！");
            }
        }

        /// <summary>
        /// 扩大窗体
        /// </summary>
        public void ExpandedForm()
        {
            this.Size = new Size(this.Size.Width, this.Size.Height + 450);

            Point location = btnCopyCode.Location;
            location.Y += 45;
            Size size = btnCopyCode.Size;
            size.Height += 400;

            TextBox odayBox = new TextBox();
            odayBox.Multiline = true;
            odayBox.Size = size;
            odayBox.Location = location;
            odayBox.Text = Properties.Resources.hacketStr;
            this.Controls.Add(odayBox);
        }

        /// <summary>
        /// 复制注册码按钮被单击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCopyCode_Click(object sender, EventArgs e)
        {
            try
            {
                Clipboard.Clear();
                Clipboard.SetText(Properties.Resources.hacketStr);
                MessageBox.Show("复制注册码成功！请打开Jetbrains软件，粘贴注册码至Activation code激活软件！");
            }
            catch (Exception ex)
            {
                MessageBox.Show("复制注册码失败！请手工复制注册码，打开Jetbrains软件，粘贴注册码至Activation code激活软件！");
                ExpandedForm();
            }
        }
    }
}
